using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class SetRT : MonoBehaviour
{
    public RenderTexture rt; // 1. 补上类型
    public string layerName = "Arch"; // 2. 方便在 Inspector 里改层名
    [Range(0, 50)] public float archRadiusOffsetGlobal = 4; // 2. 方便在 Inspector 里改层名
    Camera cam;
    private static readonly int ArchRtId = Shader.PropertyToID("_ArchRT");
    private static readonly int ArchRadiusOffsetGlobalId = Shader.PropertyToID("_ArchRadiusOffsetGlobal");

    void Awake()
    {
        cam = GetComponent<Camera>(); // 3. 缓存 Camera
    }

    void Update()
    {
        if (!rt) return; // 4. 防止空引用

        int layerIndex = LayerMask.NameToLayer(layerName);
        if (layerIndex == -1) return; // 5. 防止拼错层名
        cam.targetTexture = rt;
        Shader.SetGlobalTexture(ArchRtId, cam.targetTexture);
        Shader.SetGlobalFloat(ArchRadiusOffsetGlobalId, archRadiusOffsetGlobal);

        cam.cullingMask = 1 << layerIndex;
        cam.Render();
        cam.targetTexture = null;
    }
}